package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwaeT;
import cn.e4j.modular.crm.entity.GwaelT;
import cn.e4j.modular.crm.entity.GwazT;
import cn.e4j.modular.crm.mapper.GwazTMapper;
import cn.e4j.modular.crm.service.GwazTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liuw
 * @since 2019-08-30
 */
@Service
public class GwazTServiceImpl extends ServiceImpl<GwazTMapper, GwazT> implements GwazTService {
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwazlist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwazDtos = this.baseMapper.listGwazPage(page, dto);

        return gwazDtos;
    }

    @Transactional(readOnly = true)
    public GwazT getByKey(String GWAZENT, String GWAZ001, String GWAZLEGAL) {
        UpdateWrapper<GwazT> gwazTUpdateWrapper = new UpdateWrapper<>();
        gwazTUpdateWrapper.eq("GWAZENT", GWAZENT);
        gwazTUpdateWrapper.eq("GWAZ001", GWAZ001);
        gwazTUpdateWrapper.eq("GWAZLEGAL", GWAZLEGAL);
        GwazT gwaz = this.baseMapper.selectOne(gwazTUpdateWrapper);
        return gwaz;
    }

    @Transactional
    public void add(GwazT gwaz) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwaz.setGWAZENT(shiroUser.getDBC());
        gwaz.insert();
    }

    @Transactional
    public void edit(GwazT gwaz) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwazT> gwazTUpdateWrapper = new UpdateWrapper<>();
        gwazTUpdateWrapper.eq("GWAZENT", shiroUser.getDBC());
        gwazTUpdateWrapper.eq("GWAZ001", gwaz.getGWAZ001());
        gwazTUpdateWrapper.eq("GWAZLEGAL", gwaz.getGWAZLEGAL());
        gwaz.update(gwazTUpdateWrapper);
    }


    @Transactional
    public void delete(GwazT gwaz) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwazT> gwazTUpdateWrapper = new UpdateWrapper<>();
        gwazTUpdateWrapper.eq("GWAZENT", shiroUser.getDBC());
        gwazTUpdateWrapper.eq("GWAZ001", gwaz.getGWAZ001());
        gwazTUpdateWrapper.eq("GWAZLEGAL", gwaz.getGWAZLEGAL());
        gwaz.delete(gwazTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwazT> gwazTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwazT gwazT : gwazTList) {
            UpdateWrapper<GwazT> gwazTUpdateWrapper = new UpdateWrapper<>();
            gwazTUpdateWrapper.eq("GWAZENT", shiroUser.getDBC());
            gwazTUpdateWrapper.eq("GWAZ001", gwazT.getGWAZ001());
            gwazTUpdateWrapper.eq("GWAZLEGAL", gwazT.getGWAZLEGAL());
            gwazT.delete(gwazTUpdateWrapper);
        }
    }
}
